$(document).ready(function() {

	var storyId = $.getUrlVar('id');
	var taskUrl = $.getUrlVar('taskUrl');
	$('#storyHeader').text('Story ' + storyId);
	
	if(taskUrl == "-1"){
		$('#taskDiv').hide();
	}
});

function showError(strError, errorLocation){
	errorLocation.html(strError);
}

function saveStory(){
	
	var storyName = $('#storyName').val();
	if (!storyName.trim()) {
	    // is empty or whitespace
		showError("Please enter a story name!", $("#storyError"));
		return;
	}
	var storyDesc = $('#storyDesc').val();
	if (!storyDesc.trim()) {
		showError("Please enter a description!", $("#storyError"));
		return;
	}
	var iterationId = $('#iterationID').val();
	var storyId = $.getUrlVar('id');

	if(storyId == 0){
		$.ajax({
			url: 'backend/create_story.php',
			type: 'post',
			data: {'story_name': storyName, 'story_desc': storyDesc, 'iteration_id': iterationId },
			success: function(data){
				location.href = "story.php?id=" + data.story_id +"&iter=" + iterationId;
			},
			error: function(xhr, desc, err){
				console.log(xhr);
			}
		});
	}
	else{
		$.ajax({
			url: "backend/save_story.php",
			type: 'post',
			data: {'story_name': storyName, 'story_desc': storyDesc, 'story_id': storyId},
			success: function(data){
				location.href = "story.php?id=" + data.story_id+"&iter=" + iterationId;
			},
			error: function(xhr, desc, err){
				console.log(xhr);
			}
		});
	}
}

function createNewTask(){
	var storyId = $.getUrlVar('id');
	var iterationId = $.getUrlVar('iter');

	$.ajax({
		url: "backend/select_new_task.php",
		type: 'post',
		data: {'taskUrl': 0,
			'id': parseInt(storyId)},
		success: function(data){
			console.log(data);
			var obj = data;
			location.href = "story.php?id=" + obj.story_id + "&iter=" + iterationId + "&taskUrl=0";
		},
		error: function(xhr, desc, err){
			console.log(xhr);
	        console.log("Details: " + desc + "\nError:" + err);
		}
	});
}

function saveTask(){
	var taskId = $.getUrlVar('taskUrl');
	var storyId = $.getUrlVar('id');
	var iterationId = $.getUrlVar('iter');
	
	//Do validation
	if(!$('#taskName').val().trim()){
		showError("Please enter a task name!", $("#taskError"));
		return;
		
	}
	if (!$('#taskDescription').val().trim()) {
		showError("Please enter a description!", $("#taskError"));
		return;

	}
	if (!$('#estimatedHours').val().trim()) {
		showError("Please enter estimated hours!", $("#taskError"));
		return;

	} else {
		if (Math.floor($('#estimatedHours').val()) != $('#estimatedHours').val()
				|| !$.isNumeric($('#estimatedHours').val())) {
			showError("Please enter a numeric number for estimated hours!", $("#taskError"));
			return;
		}
	}
	if (!$('#actualHours').val().trim()) {
		showError("Please enter actual hours!", $("#taskError"));
		return;

	} else {
		if (Math.floor($('#actualHours').val()) != $('#actualHours').val()
				|| !$.isNumeric($('#actualHours').val())) {
			showError("Please enter a numeric number for actual hours!", $("#taskError"));
			return;
		}
	}
	if (!$('#status').val().trim()) {
		showError("Please enter a status!", $("#taskError"));
		return;

	}
	console.log(taskId);
	
	var undefinedTask = typeof taskId === 'undefined';
	
	if(taskId != 0 && !undefinedTask) {
		$.ajax({
			url: "backend/save_task.php",
			type: 'post',
			data: {'task_id': taskId,
				'task_name': $('#taskName').val(),
				'task_description': $('#taskDescription').val(),
				'estimated_hours' : $('#estimatedHours').val(),
				'actual_hours' : $('#actualHours').val(),
				'status' : $('#status').val()},
			success: function(data){
				location.href = "story.php?id=" + storyId + "&iter=" + iterationId + "&taskUrl=" + taskId;
			},
			error: function(xhr, desc, err){
				console.log(xhr);
		        console.log("Details: " + desc + "\nError:" + err);
			}
		});
	} else {
		$.ajax({
			url: "backend/create_task.php",
			type: 'post',
			data: {'task_name': $('#taskName').val(),
				'task_description': $('#taskDescription').val(),
				'estimated_hours' : $('#estimatedHours').val(),
				'status' : $('#status').val(),
				'story_id' : storyId},
			success: function(data){
				location.href = "story.php?id=" + storyId + "&iter=" + iterationId + "&taskUrl=" + data.task_id;
			},
			error: function(xhr, desc, err){
				console.log(xhr);
		        console.log("Details: " + desc + "\nError:" + err);
			}
		});
	}
}